<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<title>订单后台</title>
	</head>
	<body>
		<center>
		<?PHP
			date_default_timezone_set("PRC");

			include("../../public/config.php");
			$link = mysqli_connect(HOST,USER,PASS,DBNAME) or die("数据连接失败！");
			mysqli_set_charset($link,"utf8");
			switch ($_GET['a']) {							//根据a的传值进行对应的操作
				
				case 'confirm':									//对订单进行操作
					$id = $_GET['id'];
					$sql = "select * from orders where id='{$id}'";	//判断是不是无效订单
					$res = mysqli_query($link,$sql);				
					$con = mysqli_fetch_assoc($res);

					if($con[status]==3){						//如果是无效订单则不允许确认收货
						echo '<script>alert("无效订单，不能再发货！"); </script>';
						header("refresh:0;url=index.php");
						exit();
					}
					if($con[status]==2){						//如果是无效订单则不允许确认收货
						echo '<script>alert("客户已收货！"); </script>';
						header("refresh:0;url=index.php");
						exit();
					}

					//修改订单状态
					$sql = "update orders set status='1' where id={$id}";
					mysqli_query($link,$sql);
					if(mysqli_affected_rows($link)>0){
						header("Location:".$_SERVER["HTTP_REFERER"]);	//跳转返回
						exit();
					}else{
						echo '<script>alert("已发货！"); </script>';
						header("refresh:0;url=index.php");
						exit();
					}
					break;


				case 'update':						//修改订单信息
					$id = $_POST['id'];				//获取值
					$linkman = $_POST['linkman'];
					$address = $_POST['address'];
					$phone = $_POST['phone'];
					$status = $_POST['status'];


					$sql = "select * from orders where id={$id}";	//根据条件搜索数据库
					$res = mysqli_query($link,$sql);
					$rows = mysqli_fetch_assoc($res);
					$code = $_POST['code']?$_POST['code']:$rows['code'];	//判断有无输入code值


					if($linkman==""){									//判断输入值是否为空
						echo '<script>alert("联系人不能为空"); </script>';
						header("refresh:0;url=edit.php?id={$id}");
						exit();
					}
					if ($address=="") {									//判断输入值是否为空
						echo '<script>alert("收货地址不能为空"); </script>';
						header("refresh:0;url=edit.php?id={$id}");
						exit();
					}
					if ($phone=="") {									//判断输入值是否为空
						echo '<script>alert("联系电话不能为空"); </script>';
						header("refresh:0;url=edit.php?id={$id}");
						exit();
					}
																		//更新订单信息
					$sql = "update orders set linkman='{$linkman}',address='{$address}',code='{$code}',phone='{$phone}',status='{$status}' where id='{$id}'";
					mysqli_query($link,$sql);
					if(mysqli_affected_rows($link)>0){
						echo '<script>alert("修改成功"); </script>';
						header("refresh:0;url=index.php");
					}else{
						// echo $sql;
						echo "修改失败";
						header("refresh:0;url=index.php");
					}
					break;
			}
		?>
		</center>
	</body>
</html>